<template>
  <!-- 分类参数页面的tab面板 -->
  <el-tabs v-model="activeName" @tab-click="handleClick">
    <el-tab-pane label="动态参数" name="many">
      <el-button type="primary" :disabled="isBtnShow" @click="showDialog"
        >添加参数</el-button
      >
      <!-- 动态参数表格区域 -->
      <el-table :data="tabledata" style="width: 100%" border>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-tag
              :key="index"
              v-for="(item, index) in scope.row.attr_vals"
              closable
              :disable-transitions="false"
              @close="handleClose(scope.row, index)"
            >
              {{ item }}
            </el-tag>
            <el-input
              class="input-new-tag"
              v-if="scope.row.inputVisible"
              v-model="scope.row.inputValue"
              ref="saveTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm(scope.row)"
              @blur="handleInputConfirm(scope.row)"
            >
            </el-input>
            <el-button
              v-else
              class="button-new-tag"
              size="small"
              @click="showInput(scope.row)"
              >+ New Tag</el-button
            >
          </template>
        </el-table-column>
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="attr_name" label="参数名称"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="showEditDialog(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="deleteBtn(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-tab-pane>
    <el-tab-pane label="静态属性" name="only">
      <el-button type="primary" :disabled="isBtnShow" @click="showDialog"
        >添加属性</el-button
      >

      <!-- 静态属性表格区域 -->
      <el-table :data="tabledata" style="width: 100%" border>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-tag
              :key="index"
              v-for="(item, index) in scope.row.attr_vals"
              closable
              :disable-transitions="false"
              @close="handleClose(scope.row, index)"
            >
              {{ item }}
            </el-tag>
            <el-input
              class="input-new-tag"
              v-if="scope.row.inputVisible"
              v-model="scope.row.inputValue"
              ref="saveTagInput"
              size="small"
              @keyup.enter.native="handleInputConfirm(scope.row)"
              @blur="handleInputConfirm(scope.row)"
            >
            </el-input>
            <el-button
              v-else
              class="button-new-tag"
              size="small"
              @click="showInput(scope.row)"
              >+ New Tag</el-button
            >
          </template>
        </el-table-column>
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="attr_name" label="参数名称"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="showEditDialog(scope.row)"
              >编辑</el-button
            >
            <el-button size="mini" type="danger" @click="deleteBtn(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-tab-pane>
    <Dialog
      :isshowDialog="isshowDialog"
      :cateId="cateId"
      @closeDialog="closeDialog"
      @definePost="getData(tabName, cateId)"
      :tabName="tabName"
    ></Dialog>
    <!-- 编辑参数对话框 -->
    <EditDialog
      :editDialogVisible="editDialogVisible"
      :tabName="tabName"
      :cateId="cateId"
      :attrId="attrId"
      @hanleCloseEdit="hanleCloseEdit"
      @getData="getData"
    ></EditDialog>
  </el-tabs>
</template>

<script>
import { request } from "@/utils";
import Dialog from "@/components/addParamsDialog";
import EditDialog from "@/components/editParamsDialog";
export default {
  components: { Dialog, EditDialog },
  props: ["selectedOptions"],
  data() {
    return {
      cateId: 0,
      activeName: "many",
      tabledata: [],
      // 是否展示添加参数对话框
      isshowDialog: false,
      // 当前选择的标签页
      tabName: "many",
      // 是否展示编辑对话框
      editDialogVisible: false,
      // 点击编辑按钮之后当前的属性id
      attrId: 0,
    };
  },
  watch: {},
  computed: {
    isBtnShow() {
      if (this.selectedOptions.length !== 3) {
        return true;
      }
      return false;
    },
  },
  methods: {
    // 发起请求
    async getData(param, Id) {
      this.cateId = Id;
      const { data: ret } = await request.get(`categories/${Id}/attributes`, {
        params: {
          sel: [param],
        },
      });
      ret.data.forEach((item) => {
        item.attr_vals = item.attr_vals ? item.attr_vals.split(" ") : [];
        // 控制文本框的显示与隐藏
        item.inputVisible = false;
        item.inputValue = "";
      });
      this.tabledata = ret.data;
    },

    handleClick(e) {
      // 输出当前所选标签
      if (this.selectedOptions.length == 0) {
        this.$message.error("请选择商品分类");
        return;
      }
      this.tabName = e.name;
      this.getData(e.name, this.cateId);
    },
    // 展示添加属性对话框
    showDialog() {
      this.isshowDialog = true;
    },
    closeDialog() {
      this.isshowDialog = false;
    },
    showInput(row) {
      row.inputVisible = true;
      // 方法作用：页面上页面元素被重新渲染之后执行回调函数
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    // 编辑提交参数
    async saveRemote(id, row) {
      const { data: ret } = await request.put(
        `categories/${id}/attributes/${row.attr_id}`,
        {
          attr_name: row.attr_name,
          attr_sel: row.attr_sel,
          attr_vals: row.attr_vals.join(" "),
        }
      );
      console.log(ret);
    },

    handleInputConfirm(row) {
      if (row.inputValue.trim().length == 0) {
        row.inputValue = "";
        row.inputVisible = false;
        return;
      }
      // 当输入内容有效时的处理事件
      row.attr_vals.push(row.inputValue.trim());
      this.saveRemote(this.cateId, row);
      row.inputValue = "";
      row.inputVisible = false;
    },
    // 删除标签
    handleClose(row, index) {
      row.attr_vals.splice(index, 1);
      this.saveRemote(this.cateId, row);
    },
    // 删除参数
    deleteBtn(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await request
            .delete(`categories/${row.cat_id}/attributes/${row.attr_id}`)
            .then(() => {
              this.getData(this.tabName, row.cat_id);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 展示编辑对话框
    showEditDialog(row) {
      this.attrId = row.attr_id;
      this.editDialogVisible = true;
    },
    // 关闭编辑对话框
    hanleCloseEdit() {
      this.editDialogVisible = false;
    },
  },
  created() {},
  mounted() {},
};
</script>
<style lang="less" scoped>
.el-tag {
  margin-right: 15px;
}
.input-new-tag {
  width: 200px;
}

.button-new-tag {
  margin-top: 10px;
}
.input-new-tag {
  margin-top: 10px;
}
</style>